#!/usr/bin/env bash
#
# Travis CI Scripts
# Copyright (C) 2018-2020 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@iem.uni-due.de

# Bash options:
set -e


. `dirname $0`/get-container


# ###### Linux as-is ########################################################
if [ "${TRAVIS_OS_NAME}" == "linux" -a "${DOCKER}" == "" -a "${QEMU}" == "" ] ; then
   # ====== Configure with CMake ============================================
   if [ -e CMakeLists.txt ] ; then
      env CC=${COMPILER_C} CXX=${COMPILER_CXX} cmake . $@

   # ====== Configure with autoconf/automake (via bootstrap script) =========
   elif [ -e configure.ac -o -e configure.in ] ; then
      if [ -e bootstrap ] ; then
         env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./bootstrap
         env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
      elif [ -e autogen.sh ] ; then
         env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./autogen.sh
      else
         env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
      fi
   else
      echo >&2 "WARNING: No build system detected. Trying to just call \"make\" ..."
   fi
   cores=`getconf _NPROCESSORS_ONLN 2>/dev/null || true`
   if [ $cores -gt 1 ] ; then
      MAKEFLAGS="-j${cores}"
   fi

   # ====== Compile ======================================================
   if [ "${TOOL}" == "compile" ] ; then
      env MAKEFLAGS=${MAKEFLAGS} make   # VERBOSE=1

   # ====== Coverity Scan ================================================
   elif [ "${TOOL}" == "coverity" ] ; then
      # ------ Build -----------------------------------------------------
      cd coverity
      export PATH="coverity/$(ls -d cov*)/bin:$PATH"
      cd ..

      env MAKEFLAGS=${MAKEFLAGS} cov-build --dir cov-int make
      tar czf coverity-results.tar.gz cov-int
      ls -l coverity-results.tar.gz

      # ------ Upload results --------------------------------------------
      if [ "${TRAVIS_BRANCH}" == "${COVERITY_SCAN_BRANCH}" ] ; then
         curl --form token=${COVERITY_SCAN_TOKEN} \
              --form email=${COVERITY_SCAN_NOTIFICATION_EMAIL} \
              --form file=@coverity-results.tar.gz \
              --form version="master branch head" \
              --form description="$(git log -1|head -1)" \
              https://scan.coverity.com/builds?project=${COVERITY_PROJECT}
         CURL_RESULT=$?
         echo "curl returned ${CURL_RESULT}"
         if [ $CURL_RESULT -ne 0 ]; then
            echo >&2 "ERROR: Upload to Coverity Scan failed; curl returned ${CURL_RESULT}!"
            exit 1
         fi
      else
         echo >&2 "###### NOTE: This branch \"${TRAVIS_BRANCH}\" is not the scan branch \"${COVERITY_SCAN_BRANCH}\"! Skipping upload! ######"
      fi

   # ====== Invalid setting =================================================
   else
      echo >&2 "ERROR: Invalid setting of TOOL=${TOOL}!"
      exit 1
   fi


# ###### FreeBSD via QEMU ###################################################
elif [ "${TRAVIS_OS_NAME}" == "linux" -a "${QEMU}" == "FreeBSD" ] ; then

   ssh -p 8829 -oStrictHostKeyChecking=no -i ~/.ssh/id_rsa root@localhost <<EOF
      cd /travis

      # ====== Configure ====================================================
      if [ -e CMakeLists.txt ] ; then
         env CC=${COMPILER_C} CXX=${COMPILER_CXX} cmake . $@
      elif [ -e configure.ac -o -e configure.in ] ; then
         if [ -e bootstrap ] ; then
            env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./bootstrap
            env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
         elif [ -e autogen.sh ] ; then
            env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./autogen.sh
         else
            env CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
         fi
      else
         echo >&2 "WARNING: No build system detected. Trying to just call \"make\" ..."
      fi

      cores=\`sysctl -a | grep 'hw.ncpu' | cut -d ':' -f2 | tr -d ' '\`
      if [ \${cores} -gt 1 ] ; then
         MAKEFLAGS="-j\${cores}"
      fi
      echo "Running: env MAKEFLAGS=\${MAKEFLAGS} make"
      env MAKEFLAGS=\${MAKEFLAGS} make || exit 1

      echo "Done!"
      exit 0
EOF


# ###### Linux via Docker ###################################################
elif [ "${TRAVIS_OS_NAME}" == "linux" -a "${DOCKER}" != "" -a "${QEMU}" == "" ] ; then

   # ====== Compile (generic) ===============================================
   if [ "${TOOL}" == "compile" ] ; then
      echo "###### Compiling ... ##################################################"

      if [ -e CMakeLists.txt ] ; then
         sudo docker exec ${CONTAINER} env LANG=C.UTF CC=${COMPILER_C} CXX=${COMPILER_CXX} cmake . $@
      elif [ -e configure.ac -o -e configure.in ] ; then
         if [ -e bootstrap ] ; then
            sudo docker exec ${CONTAINER} env LANG=C.UTF CC=${COMPILER_C} CXX=${COMPILER_CXX} ./bootstrap
            sudo docker exec ${CONTAINER} env LANG=C.UTF CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
         elif [ -e autogen.sh ] ; then
            sudo docker exec ${CONTAINER} env LANG=C.UTF CC=${COMPILER_C} CXX=${COMPILER_CXX} ./autogen.sh
         else
            sudo docker exec ${CONTAINER} env LANG=C.UTF CC=${COMPILER_C} CXX=${COMPILER_CXX} ./configure $@
         fi
      else
         echo >&2 "WARNING: No build system detected. Trying to just call \"make\" ..."
      fi

      cores=`sudo docker exec ${CONTAINER} env LANG=C.UTF-8 getconf _NPROCESSORS_ONLN 2>/dev/null || true`
      if [ $cores -gt 1 ] ; then
         MAKEFLAGS="-j${cores}"
      fi
      sudo docker exec ${CONTAINER} env LANG=C.UTF-8 MAKEFLAGS=${MAKEFLAGS} make   # VERBOSE=1

   # ====== pbuilder (Debian/Ubuntu variants) ===============================
   elif [ "${TOOL}" == "pbuilder" ] ; then
      echo "###### Running pbuilder ... ###########################################"
      ci/monitor 60 sudo docker exec ${CONTAINER} env LANG=C.UTF-8 OVERRIDE_SKIP_PACKAGE_SIGNING=1 eatmydata ./build-deb

   # ====== mock (Fedora variants) ==========================================
   elif [ "${TOOL}" == "mock" ] ; then
      echo "###### Running mock ... ###############################################"
      release=`sudo docker exec -t ${CONTAINER} bash -c "LANG=C.UTF-8 ; cat /etc/fedora-release | sed -e \"s/^\(.*\) release \([0-9]*\) (\(.*\))$/\2/g\"" | sed -e "s/[^0-9]//g"`
      arch=`sudo docker exec -t ${CONTAINER} env LANG=C.UTF-8 uname -m | sed -e "s/[^0-9a-zA-Z_+-]//g"`
      ci/monitor 60 sudo docker exec ${CONTAINER} env LANG=C.UTF-8 LD_PRELOAD=/usr/lib64/nosync/nosync.so OVERRIDE_SKIP_PACKAGE_SIGNING=1 ./build-rpm fedora-${release}-${arch}

   # ====== Invalid setting =================================================
   else
      echo >&2 "ERROR: Invalid setting of TOOL=${TOOL}!"
      exit 1
   fi


# ###### MacOS X ############################################################
elif [ "${TRAVIS_OS_NAME}" == "osx" ] ; then

   cmake . $@
   make VERBOSE=1


# ###### Error ##############################################################
else
   echo >&2 "ERROR: Invalid setting of TRAVIS_OS_NAME=${TRAVIS_OS_NAME}, DOCKER=${DOCKER}, QEMU=${QEMU}!"
   exit 1
fi
